import { GlobalRoot } from '../global/GlobalRoot';
import { PopupManager } from '../managers/PopupManager';
import { Node, Label, Layers } from 'cc';

/**
 * PopupManager 功能测试用例集合
 */
export class PopupTest {
    static async runAll(parentNode?: Node) {
        await this.testLoadPopup(parentNode);
        await this.testShowPopup(parentNode);
        await this.testClosePopup(parentNode);
        await this.testPopupCache(parentNode);
    }
    /**
     * 测试弹窗加载
     * 预期：控制台输出弹窗Prefab对象
     */
    static async testLoadPopup(parentNode?: Node) {
        const prefab = await PopupManager.instance.preloadPopup({ path: 'ConfirmDialog', bundle: "popups", isRemote: false });
        console.log('[测试] 弹窗Prefab加载完成', prefab);
        GlobalRoot.instance.UiManager.showTips("[测试] 弹窗Prefab加载完成")

    }
    /**
     * 测试弹窗显示
     * 预期：弹窗显示在场景中
     */
    static async testShowPopup(parentNode?: Node) {
        const node = await PopupManager.instance.showPopup({ path: 'ConfirmDialog', bundle: "popups", isRemote: false, data: { message: '测试弹窗显示' } });
        console.log('[测试] 弹窗已显示', node);
        if (parentNode) {
            parentNode.removeAllChildren();
            const labelNode = new Node('PopupShowLabel');
            GlobalRoot.instance.UiManager.showTips("[测试] 弹窗已显示")

            parentNode.addChild(labelNode);
        }
    }
    /**
     * 测试弹窗关闭
     * 预期：弹窗关闭后节点被销毁
     */
    static async testClosePopup(parentNode?: Node) {
        const config = { path: 'ConfirmDialog', bundle: "popups", isRemote: false, data: { message: '测试弹窗关闭' } };
        await PopupManager.instance.showPopup(config);
        setTimeout(() => {
            PopupManager.instance.closePopup(config);
            console.log('[测试] 弹窗已关闭', config.path);
            GlobalRoot.instance.UiManager.showTips("[测试] 弹窗已关闭")

        }, 2000);
    }
    /**
     * 测试弹窗缓存机制
     * 预期：多次打开同一弹窗Prefab只加载一次
     */
    static async testPopupCache(parentNode?: Node) {
        const prefab1 = await PopupManager.instance.preloadPopup({ path: 'ConfirmDialog', bundle: "popups", isRemote: false });
        const prefab2 = await PopupManager.instance.preloadPopup({ path: 'ConfirmDialog', bundle: "popups", isRemote: false });
        console.log('[测试] 弹窗Prefab缓存复用', prefab1 === prefab2);
        GlobalRoot.instance.UiManager.showTips(prefab1 === prefab2?"[测试] 弹窗Prefab缓存复用成功":"[测试] 弹窗Prefab缓存复用失败")

        if (parentNode) {
            parentNode.removeAllChildren();
            const labelNode = new Node('PopupCacheLabel');
            labelNode.layer = Layers.Enum.UI_2D;
            const label = labelNode.addComponent(Label);
            label.overflow = Label.Overflow.RESIZE_HEIGHT;
            label.lineHeight = 24;
            label.fontSize = 18;
            label.string = '弹窗Prefab缓存复用: ' + (prefab1 === prefab2 ? '是' : '否');
            labelNode.setPosition(0, 0);
            parentNode.addChild(labelNode);
        }
    }
} 